#ifndef DEALPICTURETHREAD_H
#define DEALPICTURETHREAD_H

#include <QObject>
#include <QMap>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <QImage>
#include <QVariant>
#include <atomic>
#include "mtpack.h"
#include <mtPackTransform.h>

/**
 * @brief The SmallPic_Struct struct
 */
struct SmallPic_Struct{
   QString id;
   QImage image;

   SmallPic_Struct(const QString id,const QImage& img){
       this->id = id;
       this->image = img;
   }
};

typedef struct FixedPackImg_Struct{
    double initWidth;
    double initHeight;
    double x;
    double y;
    double scale;
    int xDpi;
    int yDpi;
    bool isRotate;
    int jobIndex;
    std::string fileName;

    FixedPackImg_Struct(){
        this->initWidth = 0;
        this->initHeight = 0;
        this->x = 0;
        this->y = 0;
        this->scale = 0;
        this->xDpi = 0;
        this->yDpi = 0;
        this->isRotate = false;
        this->jobIndex  = 0;
        this->fileName = "";
    }

    FixedPackImg_Struct(const FixedPackImg_Struct& obj){
        this->initWidth = obj.initWidth;
        this->initHeight = obj.initHeight;
        this->x = obj.x;
        this->y = obj.y;
        this->scale = obj.scale;
        this->xDpi = obj.xDpi;
        this->yDpi = obj.yDpi;
        this->isRotate = obj.isRotate;
        this->jobIndex = obj.jobIndex;
        this->fileName = obj.fileName;
    }

    FixedPackImg_Struct& operator=(const FixedPackImg_Struct& obj){
        this->initWidth = obj.initWidth;
        this->initHeight = obj.initHeight;
        this->x = obj.x;
        this->y = obj.y;
        this->scale = obj.scale;
        this->xDpi = obj.xDpi;
        this->yDpi = obj.yDpi;
        this->isRotate = obj.isRotate;
        this->jobIndex = obj.jobIndex;
        this->fileName = obj.fileName;
        return *this;
    }
}FixedPackImg;
/*******************处理缩略图************************/
/**
 * @brief The DealSmallPictureThread class
 * @author xxz
 */
class DealSmallPictureThread:public QObject
{
    Q_OBJECT
public:
    DealSmallPictureThread(QObject* parent = nullptr);
    ~DealSmallPictureThread();

protected:
    void run();

public slots:
    void doMessageAccepted(SmallPic_Struct*);
    void doCurrentFolderPicInfo(const QString& curPath);

signals:
    void onDealCompleted();// 图片处理完毕发出该信号
    void onRecoverButtonState(const bool& flag);  //单次点击获取到所有缩略图之后，按钮状态恢复信号
    void onPicCompleted(const std::map<QString,QString>&);

private:
    std::thread* m_Thread = nullptr;
    std::queue<SmallPic_Struct*> m_MsgQueue;           //前端页面的消息队列
    std::mutex m_Mutex;
    std::condition_variable m_Condition;
    bool is_Waiting = true;                   //标记线程是还在运行
    QString m_CurPath;
    //std::atomic<int>  m_SmallPicCount{0};
    std::map<QString,QString> m_PicMd5Str;

};

/********************处理产生大图***********************/
/**
 * @brief The DealBigPictureThread class
 * @author xxz
 */
class DealBigPictureThread:public QObject
{
    Q_OBJECT
public:
    DealBigPictureThread(QObject* parent = nullptr);
    ~DealBigPictureThread();

protected:
    void run();

public slots:
    void doMessageAccepted(QVariantList&);
    void doSetPaperWidth(const QString&);
    void doBingPack(const bool&);
    void doPicGap(const double&);
    void doDealPackList(const QVariantList& list,const double& viewHeight);

signals:
    // 图片处理完毕发出该信号
    void onDealFixedPackCompleted();
    void onDealAutoPackCompleted();
    void onUpdateUI_BigPicSize(const int& w,const int& h);
    // 处理排版结果
    void onDealBingPack(MtPackTransformRet *,const QList<int>& jobIndex);
    void onDealAutoPackList(const QVariantList&);
    void onDealPaperHeight(const double&);

private:
    std::thread* m_Thread = nullptr;
    std::queue<QVariantList> m_MsgQueue;           //前端页面的消息队列
    std::mutex m_Mutex;
    std::condition_variable m_Condition;
    bool is_Waiting = true;                   //标记线程是还在运行

    QString m_PaperWidth = "330";
    double m_PicGap = 0;  //作业之间的间距
    bool isAutoBingPack = false;  //是否自动排版

    QVariantList m_PackList = {};
    double m_ViewHeight = 0.0;
};

#endif // DEALPICTURETHREAD_H
